﻿using System;

namespace DZTC.UselessAppEver.Core
{
    public class ApplicationOptions
    {
        public enum ApplicationMode
        {
            Default,
            Encode,
            Decode,
            Install,
            Uninstall
        }


        public ApplicationMode Mode { get; set; }
        public string FileName { get; set; }


        public static ApplicationOptions Parse(string[] args, Action<string> errorCallback)
        {
            // i know there is Mono.Options and kinda stuff

            var opts = new ApplicationOptions();

            if (args.Length == 0)
            {
                opts.Mode = ApplicationMode.Default;
                return opts;
            }

            var firstArg = args[0];
            if (firstArg.StartsWith("--")) // option?
            {
                var option = firstArg.Substring(2);

                try
                {
                    opts.Mode = (ApplicationMode) Enum.Parse(typeof (ApplicationMode), option, ignoreCase: true);
                }
                catch (ArgumentException)
                {
                    errorCallback(String.Format(Localization.InvalidOption, option));
                    return null;
                }

                if (opts.Mode.In(ApplicationMode.Encode, ApplicationMode.Decode))
                {
                    if (args.Length == 1)
                    {
                        errorCallback(Localization.NoFileSpecified);
                        return null;
                    }

                    opts.FileName = args[1];
                }

            }
            else // decode this file
            {
                opts.Mode = ApplicationMode.Decode;
                opts.FileName = args[0];
            }

            return opts;
        }
    }
}

